package com.li.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.li.constant.MessageConstant;
import com.li.entity.Result;
import com.li.pojo.Setmeal;
import com.li.service.SetmealService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;
    @Autowired
    JedisPool jedisPool;
    //获取所有套餐
    @RequestMapping("/getAllSetmeal")
    public Result getAll(){
        Jedis jedis = jedisPool.getResource();
        String setmealList = jedis.get("setmealList");
        //判断redis是否有值
        if(setmealList!=null){
            //将json字符串转化为对象，
            Object parse = JSON.parse(setmealList);
            jedis.close();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,parse);
        }
        try {
            List<Setmeal> list = setmealService.getAll();
            //对象转json，存储于redis
            String listAsJson = JSON.toJSONString(list);
            jedis.setex("setmealList",60*60*24*7,listAsJson);
            jedis.close();
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            jedis.close();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        //防止奇葩的输入
        if (id <= 0) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
        Jedis jedis = jedisPool.getResource();
        String setmealById = jedis.get("setmealBy" + id);
        //判断redis是否有值
        if (setmealById != null) {
            //将json字符串转化为对象，
            Object parse = JSON.parse(setmealById);
            jedis.close();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, parse);
        }
        try {
            Setmeal setmeal = setmealService.findSetmealById(id);
            //为防止缓存雪崩设置7到9天的随机数不一
            int time =new Random().nextInt(24*60*60*2)+24*60*60*7;
            //对象转json，存储于redis
            String setmealByIdAsJson = JSON.toJSONString(setmeal);
            jedis.setex("setmealBy" + id, time, setmealByIdAsJson);
            jedis.close();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            //防止缓存穿透
            jedis.setex("setmealBy" + id, 60 * 60 * 24, "你的输入不合法或未能搜索到相关信息");
            jedis.close();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/getAllHot")
    public Result getAllHot(){
        Jedis jedis = jedisPool.getResource();
        String setmealList = jedis.get("setmealHotList");
        //判断redis是否有值
        if(setmealList!=null){
            //将json字符串转化为对象，
            Object parse = JSON.parse(setmealList);
            jedis.close();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,parse);
        }
        try {
            List<Setmeal> list = setmealService.getAllHot();
            //对象转json，存储于redis
            String listAsJson = JSON.toJSONString(list);
            jedis.setex("setmealHotList", 60 * 60 * 24 * 7, listAsJson);
            jedis.close();
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            jedis.close();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/getAllUpdate")
    public Result getAllUpdate(){
        Jedis jedis = jedisPool.getResource();
        String setmealList = jedis.get("setmealUpdateList");
        //判断redis是否有值
        if(setmealList!=null){
            //将json字符串转化为对象，
            Object parse = JSON.parse(setmealList);
            jedis.close();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,parse);
        }
        try {
            List<Setmeal> list = setmealService.getAllUpdate();
            //对象转json，存储于redis
            String listAsJson = JSON.toJSONString(list);
            jedis.setex("setmealUpdateList",60*60*24*7,listAsJson);
            jedis.close();
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            jedis.close();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/findByCondition")
    public Result findByCondition(String queryString){
        try {
            queryString= new String(queryString.getBytes("ISO-8859-1"), "utf-8");
            List<Setmeal> list= setmealService.findByCondition(queryString);
            if(list!=null&&list.size()>0){
                return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
            }
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
